package debut.peinture;

import java.io.IOException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class Diaporama extends Activity implements Constantes {
	private String strsql = null;
	private Cursor curseur = null;
	private int numeroTableau = -1;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle etatSauve) {
        super.onCreate(etatSauve);
//        setContentView(R.layout.diapo);
        /* recuperation parametres de lancement */
    	Bundle bu = getIntent().getExtras();
    	if(bu != null) {
    		strsql = bu.getString(EXTRA_REQUETE);
    	}
    	if (etatSauve != null) {
        	super.onRestoreInstanceState(etatSauve);
        	numeroTableau = etatSauve.getInt("numtab");
    	}
    }
    
    @Override
    public void onStart() {
        super.onStart();
        // Ouverture base de donnees
        String monChemin = CHEMIN_DB + NOM_DB;
    	SQLiteDatabase maDataBase = SQLiteDatabase.openDatabase(monChemin, null, SQLiteDatabase.OPEN_READONLY);
    	curseur = maDataBase.rawQuery(this.getBaseContext().getResources().getString(R.string.requetetout) + strsql, null);
    	Timer periodique = new Timer();
    	periodique.schedule(new changeTableau(), 0, 4000);
    }
    
    private class changeTableau extends TimerTask {    	
    	public void run() {
    		handleur.sendEmptyMessage(0);
    	}
    }

	private Handler handleur = new Handler() {
		public void handleMessage(Message msg) {
			Bitmap img = null;
    		String[] colonnes = getResources().getStringArray(R.array.colonnes);
    		numeroTableau += 1;
    		if(!curseur.isAfterLast() && curseur.moveToPosition(numeroTableau)) {
        		String lien = curseur.getString(curseur.getColumnIndex(colonnes[0]));
        		boolean fait = false;
    			try {
//    				URL urlimage = new URL(lien);
    				img = BitmapFactory.decodeStream((new URL(lien)).openConnection().getInputStream());
    				fait = true;
    			} catch (IOException e) {
//    				Log.e(Context.ACTIVITY_SERVICE, "ImageAdapteur:getView:erreur accès image" + e.getMessage());
    			} catch (OutOfMemoryError e) {
    				fait = false;
    			}
    			View vtab = findViewById(R.id.tableau);
    			if(fait) {
    				((ImageView)vtab).setImageBitmap(img);
    				Animation animp = AnimationUtils.loadAnimation(vtab.getContext(), R.anim.rotor);
        			vtab.startAnimation(animp);
    			}
        	} else {
        		numeroTableau = -1;
        		curseur.moveToFirst();
        	}
		}
	};
	
	@Override 
    public void onSaveInstanceState(Bundle sauvEtat) {
    	sauvEtat.putInt("numtab", numeroTableau);
    	super.onSaveInstanceState(sauvEtat);
    }
   
}
